package TrafficControlSystem.Common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public abstract class Response {
	public static Response getFrom(Socket serverSocket) {
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
			String firstLine = reader.readLine();

			if (firstLine.equals("INTENSITY")) {
				String lengthStr = reader.readLine();
				int length = Integer.parseInt(lengthStr);
				char[] xmlChr = new char[length];
				reader.read(xmlChr);
				String xml = new String(xmlChr);
				return new GETINTENSITYResponse(xml);
			} else if (firstLine.equals("LIGHTS")) {
				String lengthStr = reader.readLine();
				int length = Integer.parseInt(lengthStr);
				char[] xmlChr = new char[length];
				reader.read(xmlChr);
				String xml = new String(xmlChr);
				return new GETLIGHTSResponse(xml);
			} else if (firstLine.equals("OK")) {
				return new OKResponse();
			}		
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public abstract void send(Socket clientSocket);
}
